package com.jhmk.springboot.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jhmk.springboot.mappers.UserMapper;
import com.jhmk.springboot.query.UserQuery;
import com.jhmk.springboot.utils.AssertUtil;
import com.jhmk.springboot.vo.User;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;

@Service
public class UserService {

    @Resource
    private UserMapper userMapper;

    public User queryUserByUserId(String id){

        //return userDao.queryUserByUserId(id);
        return userMapper.selectById(id);
    }
    public List<User> queryUserByUserName(String name){
        Map<String, Object> map = new HashMap<>();
        map.put("username", "admin");
         return  userMapper.selectByMap(map);
        //return userDao.queryUserByUserName(name);
    }
    @Transactional(propagation = Propagation.REQUIRED) //注解方式，事务开启
    public int addUser(User user){
        AssertUtil.isTrue(StringUtils.isBlank(user.getUsername()),"用户名不能为空！");
        AssertUtil.isTrue(StringUtils.isBlank(user.getId()),"id不能为空！");
        return userMapper.insert(user);
        //return userDao.addUser(user);
    }
    public int updateUser(User user)
    {
        return userMapper.updateById(user);
        //return userDao.updateUser(user);
    }
    public int deleteUser(String id){
        return  userMapper.deleteById(id);
        //return userDao.deleteUser(id);
    }

    public PageInfo<User> queryUserByParams(UserQuery query){
        PageHelper.startPage(query.getPageNum(),query.getPageSize());
        Map<String, Object> map = new HashMap<>();
        map.put("username", query.getUserName());
        List<User> userList= userMapper.selectByMap (map);
        return new PageInfo<User>(userList);
    }

}
